<?php
class PicturesController extends AppController {
	var $name = 'Pictures';
	var $helpers = array('Html');
	var $components = array('Init', 'Param');
	
	function beforeFilter() {
		if (!$this->Session->read('admin')) {
			$this->redirect('/users/login');
		}
		else {
			$this->Init->startup($this);
			$this->Admin = $this->Init->init();
			
			$this->set('dashboard',0);
			$this->set('content',0);
			$this->set('products',0);
			$this->set('component',1);
			$this->set('system',0);
			
			$this->set('title','Pictures Management');
			$this->set('sub_title', $this->Param->getAction($this->params));
  		}
	}
	
	function index(){
		$pictures = $this->Picture->find('all', array(
                'conditions' => array(
                        'Picture.status' => 1,
                        'Picture.kind' => 1
                        ),
                'order' 	 => array('Picture.sort desc','Picture.date desc')
                ));
		$this->set('pictures',$pictures);
        }
	
	function edit($id = null){
		$this->loadModel('Content');
		
		$id = abs(intval($id));
		
		if ($id < 1) {
			$this->redirect('/pictures');	
		}
		
		$this->set('id', $id);
		
		if (isset($this->data)) {
			$this->data['Picture']['id'] = $id;
			$this->data['Picture']['name'] = trim(strip_tags($this->data['Picture']['name']));
			$this->data['Picture']['link'] = $this->data['Picture']['link'];
			$this->data['Picture']['date'] = time();

			if ($this->Picture->save($this->data['Picture'])) {
				$this->Session->setFlash('abc');
				$this->redirect('/pictures/edit/' . $id);
			}
		}
		
		$this->data = $this->Picture->find('first',	array(
			'conditions' => array('Picture.id' => $id)
		));
		
		$menus = $this->Content->find('list', array(
			'conditions' => array('Content.kind' => 2),
			'fields' => array('Content.id', 'Content.name')
		));
		$this->set('menus',$menus);
	}
	
	function add(){
		$this->helpers[] = 'Ck';
		$this->loadModel('Content');
		$menus = $this->Content->find('list', array(
			'conditions' => array('Content.kind' => 2),
			'fields' => array('Content.id', 'Content.name')
		));
		$this->set('menus',$menus);
		if (isset($this->data)) {
			$this->data['Picture']['name'] = trim(strip_tags($this->data['Picture']['name']));
			$this->data['Picture']['date'] = date('y-m-d');
			if ($this->Picture->save($this->data['Picture'])){
				$this->Session->setFlash('Insert sucessfully');
				$this->redirect('/pictures');
			}
			else {
				$this->Session->setFlash('Insert faithfully');
				$this->redirect('/pictures');
			}
		}
	}
	
	function del($id = null) {
		 $this->Picture->id = $id;
		 $this->Picture->delete();
		 $this->Session->setFlash('Delete Successfully');
		 $this->redirect('/pictures');
	}
	
	function banner() {
		$pictures = $this->Picture->find('all', array(
										'conditions' => array(
											'Picture.status' => 1,
											'Picture.kind' => 1
											),
										'order' 	 => 'Picture.date desc'
										));
		$this->set('pictures',$pictures);
		$this->set('ac', 'banner');
		$this->render('index');
	}
	
	function ad() {
		$pictures = $this->Picture->find('all', array(
										'conditions' => array(
											'Picture.status' => 1,
											'Picture.kind' => 4											),
										'order' 	 => 'Picture.date desc'
										));
		$this->set('pictures',$pictures);
		$this->set('ac', 'ad');
		$this->render('index');
	}
	
	function inside_banner() {
		$this->Picture->loadJoin();
		$pictures = $this->Picture->find('all', array(
										'conditions' => array(
											'Picture.status' => 1,
											'Picture.kind' => 3,
											),
										'order' 	 => 'Picture.date desc'
										));
		$this->set('pictures',$pictures);
		$this->set('ac', 'inside_banner');
		$this->render('index');
	}
	
	function brand() {
		$pictures = $this->Picture->find('all', array(
										'conditions' => array(
											'Picture.status' => 1,
											'Picture.kind' => 2,
											),
										'order' 	 => 'Picture.date desc'
										));
		$this->set('pictures',$pictures);
		$this->set('ac', 'brand');
		$this->render('index');
	}
	
}	

?>